var customerCountLine, customerModifyBar, exceptionPie, layer;
layui.config({
    base: '../../portal/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use('index');
$(function () {
    layui.use(['laydate', 'element', 'form', 'layer'], function () {
        var element = layui.element;
        var laydate = layui.laydate;
        var form = layui.form;
        layer = layui.layer;
        laydate.render({
            elem: '#dateStart' //指定元素
        });
        laydate.render({
            elem: '#dateEnd' //指定元素
        });
        laydate.render({
            elem: '#modifyStateDate' //指定元素
            , value: new Date()
            , done: function (value, date, endDate) { //监听日期被切换
                showModifyBar(value);
            }
        });

        element.on('tab(customerStatsTab)', function () {
            var id = this.getAttribute('lay-id');
            var data = {"refreshType": "customerStatsLine", "customerStateType": id};
            showCustomerLine(data);
            return false;
        });

        form.on('submit(formStats)', function (data) {
            var startDate = data.field.customerStateDateStart
            var endDate = data.field.customerStateDateEnd;
            if(startDate && endDate){
                if(compareDate(startDate,endDate)){
                    layer.msg("时间选择不正确");
                }else{
                    if(compareDate(startDate,null)){
                        layer.msg("开始日期过大");
                    }else{
                        if(compareDate(endDate,null)){
                            var current = new Date().Format("yyyy-MM-dd");
                            $('#dateEnd').val(current);
                            data.field.customerStateDateEnd = current;
                        }
                        showCustomerLine(data.field);
                    }
                }
            }
            return false;
        });
    });
    customerCountLine = echarts.init(document.getElementById('customerCountLine'));
    // customerModifyBar = echarts.init(document.getElementById('customerModifyBar'));
    // exceptionPie = echarts.init(document.getElementById('exceptionPie'));

    showCustomerCount();
    var data = {"refreshType": "customerStatsLine", "customerStateType": "month"};
    showCustomerLine(data);
    // showModifyBar();
    // showExceptionPie();
});

var showCustomerCount = function () {
    var data = {"refreshType": "customerStats"};
    showCustomerStats(JSON.stringify(data), "customerStats");
};

var showCustomerLine = function (data) {
    showCustomerStats(JSON.stringify(data), "customerCountLine");
};

// var showModifyBar = function (date) {
//     var data;
//     if(date){
//         data = {"refreshType": "customerModifyBar", "modifyStateDate": date};
//     }else{
//         data = {"refreshType": "customerModifyBar"};
//     }
//     showCustomerStats(JSON.stringify(data), "customerModifyBar");
// };
//
// var showExceptionPie = function () {
//     var data = {"refreshType": "exceptionPie"};
//     showCustomerStats(JSON.stringify(data), "exceptionPie");
// };


var showCustomerStats = function (body, type) {
    $.ajax({
        type: 'post',
        headers: {"token": getCookie("token")},
        url: serverPre + "/manager/yzt/v1/home",
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        data: body || {},
        async: true,
        cache: false,
        success: function (result) {
            if (result.code == 0) {
                if (type === "customerCountLine") {
                    var a = result['data']['customerStatsLine'];
                    var b = JSON.parse(a);
                    b.color = ["#7ec8c1", "#fcb877", "#49b2c3"];
                    customerCountLine.setOption(b);
                } else if (type === "customerModifyBar") {
                    var c = JSON.parse(result['data']['customerModifyBar']);
                    c.color = ["#e87c25", "#27727b", "#ff8463"];
                    customerModifyBar.setOption(c);
                } else if (type === "customerStats") {
                    var s = result.data.customerStats.toString();
                    var map = JSON.parse(result.data.customerStats);
                    for (var i in map) {
                        var obj = map[i];
                        if (i != 'system') {
                            document.getElementById(i).innerText = obj['sum'];
                            document.getElementById(i + "Add").innerText = "今日新增" + obj['today'];
                        } else {
                            document.getElementById(i).innerText = Math.round(obj['today'] / obj['sum'] * 100000) / 1000.00 + "%";
                        }
                    }
                } else if (type === "exceptionPie") {
                    var d = JSON.parse(result['data']['exceptionPie']);
                    d.color = ["#27727b", "#ff8463"];
                    exceptionPie.setOption(d);
                }
            } else {
                layer.msg(result.msg);
            }
        }
    });
};

function compareDate(date1,date2){
    var oDate1 = new Date(date1);
    var oDate2;
    if(date2){
        oDate2 = new Date(date2);
    }else{
        oDate2 = new Date();
    }
    return oDate1.getTime() > oDate2.getTime();
}

Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
